package com.isoft.airportsystem.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.airportsystem.annotation.RequiresPermission;
import com.isoft.airportsystem.common.change.Result;
import com.isoft.airportsystem.entity.system.Role;
import com.isoft.airportsystem.service.system.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/system/role")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色列表
     */
    @RequiresPermission("system:role:list")
    @GetMapping("/list")
    public Result<Page<Role>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            Role role) {
        Page<Role> page = new Page<>(current, size);
        Page<Role> result = roleService.selectRolePage(page, role);
        return Result.success(result);
    }

    /**
     * 查询所有角色
     */
    @RequiresPermission("system:role:list")
    @GetMapping("/all")
    public Result<List<Role>> all() {
        List<Role> roles = roleService.list();
        return Result.success(roles);
    }

    /**
     * 根据ID查询角色
     */
    @RequiresPermission("system:role:query")
    @GetMapping("/{roleId}")
    public Result<Role> getById(@PathVariable Long roleId) {
        Role role = roleService.getById(roleId);
        return Result.success(role);
    }

    /**
     * 新增角色
     */
    @RequiresPermission("system:role:add")
    @PostMapping
    public Result<?> add(@RequestBody Role role, @RequestParam(required = false) Long[] permissionIds) {
        boolean result = roleService.insertRole(role, permissionIds);
        return result ? Result.success("新增成功") : Result.fail("新增失败");
    }

    /**
     * 修改角色
     */
    @RequiresPermission("system:role:edit")
    @PutMapping
    public Result<?> update(@RequestBody Role role, @RequestParam(required = false) Long[] permissionIds) {
        boolean result = roleService.updateRole(role, permissionIds);
        return result ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 删除角色
     */
    @RequiresPermission("system:role:remove")
    @DeleteMapping("/{roleId}")
    public Result<?> delete(@PathVariable Long roleId) {
        boolean result = roleService.deleteRoleById(roleId);
        return result ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 批量删除角色
     */
    @RequiresPermission("system:role:remove")
    @DeleteMapping("/batch/{roleIds}")
    public Result<?> batchDelete(@PathVariable Long[] roleIds) {
        boolean result = roleService.deleteRoleByIds(roleIds);
        return result ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询角色权限ID列表
     */
    @RequiresPermission("system:role:query")
    @GetMapping("/{roleId}/permissions")
    public Result<List<Long>> getRolePermissions(@PathVariable Long roleId) {
        List<Long> permissionIds = roleService.selectPermissionIdsByRoleId(roleId);
        return Result.success(permissionIds);
    }
}